package com.sir.tao.carhire.log;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
/**
 * @author dyy
 * @data 2021/12/13 18:25
 * @description:
 */
@Aspect
@Component
public class LogAspect {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 切入点
     */
    @Pointcut("execution(* com.sir.tao.carhire.controller.*.*(..))")
    public void log() {

    }

    /**
     * 方法请求前
     */
    @Before("log()")
    public void doBefore() {
        log.info("-------来请求了-------");
    }

    /**
     * 方法请求后
     * 说明：用户信息打印
     * @param joinPoint 连接点
     */
    @After("log()")
    public void doAfter(JoinPoint joinPoint) {

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert attributes != null;
        HttpServletRequest request = attributes.getRequest();
        final String ipAddr = IpUtils.getIpAddr(request);
        String url = request.getRequestURL().toString();
        String ip = request.getRemoteAddr();
        String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
//        RequestMethod requestLog = new RequestMethod(url, ip, classMethod, args);

        log.info("Request : ************************************{}","");
    }

    /**
     * 返回执行结果，在执行方法后返回
     * @param result 请求
     */
    @AfterReturning(returning = "result", pointcut = "log()")
    public void doAfterReturn(Object result) {
        log.info("Result : {}", result);
    }

}
